//
//  KVOArray.m
//  UKListManager
//
//  Created by ET|冰琳 on 16/11/11.
//  Copyright © 2016年 UK. All rights reserved.
//

#import "KVOArray.h"

@interface KVOArray (){
    NSMutableArray *_array;
}

@property (nonatomic, strong) NSMutableArray *toObserver;

@end

@implementation KVOArray

- (instancetype)init{
    if (self = [super init]) {
        self.toObserver = [NSMutableArray new];
        [self addObserver:self forKeyPath:@"toObserver" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (NSMutableArray *)array{
    if (!_array) {
        @synchronized (self) {
            _array = [self mutableArrayValueForKey:@"toObserver"];
        }
    }
    return _array;
}

- (void)setArray:(NSMutableArray *)kvoArray{
    self.toObserver = kvoArray;
    //    _kvoArray = nil;
    //    self.kvoArray = [self mutableArrayValueForKey:@"originArray"];
}

- (void)dealloc{
    NSLog(@"%@",@"dealloc");
    [self removeObserver:self forKeyPath:@"toObserver"];
}

#pragma mark - kvo
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    //NSLog(@"KVOArray %@, \n %@, \n %@",keyPath, object, change);
}


#pragma mark - data
- (void)addObject:(id)item{
    [self.array addObject:item];
}

- (void)removeAllObjects{
    [self.array removeAllObjects];
}

- (void)addObjectsFromArray:(NSArray *)otherArray{
    [self.array addObjectsFromArray:otherArray];
}


@end
